package com.hly.tool.number;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ：Jack Hao
 * @date ：Created in 2021/10/5 14:38
 * @description：判断字符串的数据类型。
 */
public class NumberValidationUtils
{


    /**
     * 正则表达式匹配
     * @param regex
     * @param orginal
     * @return
     */
    private static boolean isMatch(String regex, String orginal)
    {
        if (orginal == null || orginal.trim().equals(""))
        {
            return false;
        }
        Pattern pattern = Pattern.compile(regex);
        Matcher isNum = pattern.matcher(orginal);
        return isNum.matches();
    }

    /**
     * 判断字符串为正整数
     *
     * @param orginal
     * @return
     */
    public static boolean isPositiveInteger(String orginal)
    {
        return isMatch("^\\+{0,1}[1-9]\\d*", orginal);
    }

    /**
     * 判断字符串为负整数
     *
     * @param orginal
     * @return
     */
    public static boolean isNegativeInteger(String orginal)
    {
        return isMatch("^-[1-9]\\d*", orginal);
    }

    /**
     * 判断字符串为整数
     *
     * @param orginal
     * @return
     */
    public static boolean isWholeNumber(String orginal)
    {
        return isMatch("[+-]{0,1}0", orginal) || isPositiveInteger(orginal) || isNegativeInteger(orginal);
    }

    /**
     * 判断字符串为正小数
     *
     * @param orginal
     * @return
     */
    public static boolean isPositiveDecimal(String orginal)
    {
        return isMatch("\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*", orginal);
    }

    /**
     * 判断字符串为负小数
     *
     * @param orginal
     * @return
     */
    public static boolean isNegativeDecimal(String orginal)
    {
        return isMatch("^-[0]\\.[1-9]*|^-[1-9]\\d*\\.\\d*", orginal);
    }

    /**
     * 判断字符串为小数
     *
     * @param orginal
     * @return
     */
    public static boolean isDecimal(String orginal)
    {
        return isMatch("[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+", orginal);
    }

    /**
     * 判断字符串为实数
     *
     * @param orginal
     * @return
     */
    public static boolean isRealNumber(String orginal)
    {
        return isWholeNumber(orginal) || isDecimal(orginal);
    }
}
